package org.liao.springboot.webservice.ws.client;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.liao.springboot.webservice.ws.service.WeatherInfoReq;

import javax.xml.namespace.QName;
import java.lang.reflect.Method;

public class JaxWsDynamicClientFactoryClient {
    public static void main(String[] args) throws Exception {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:1899/weather?wsdl",WeatherInfoReq.class.getClassLoader());

        client.getEndpoint().getOutInterceptors().add(new LoggingOutInterceptor());

        QName operationName = new QName("http://service.ws.webservice.springboot.liao.org/","getWeatherByCity");

        Object weatherInfoReq = Thread.currentThread().getContextClassLoader().loadClass("org.liao.springboot.webservice.ws.service.WeatherInfoReq").newInstance();

        Method m = weatherInfoReq.getClass().getMethod("setCity", String.class);
        m.invoke(weatherInfoReq, "三门峡");
        m = weatherInfoReq.getClass().getMethod("setSendTime", Long.class);
        m.invoke(weatherInfoReq, System.currentTimeMillis());
        Object[] getWeatherByCities = client.invoke(operationName, weatherInfoReq);
        System.out.println(getWeatherByCities[0].toString());

    }
}
